﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eProject.Business;

namespace eProject.Web.UI
{
    public partial class AirportsManagementPro : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            PopulateControls();
        }
        private void PopulateControls()
        {
            if (Page.IsPostBack) return;
            BindAirportsSearch();
        }
        private void BindAirports()
        {
            try
            {
                grvAirport.DataSource = AirPorts.GetAll();
                grvAirport.DataBind();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
        private void BindAirportsSearch() {
            try {
                bool delete = cbDelete.Checked;
                string text = txtSearchText.Text.Trim().ToString();
                grvAirport.DataSource = AirPorts.getSearch(text, delete);
                grvAirport.DataBind();
            }catch(Exception ex){
                throw ex;
            }
        }
        public List<Cities> BindCities()
        {
            return Cities.GetAll();
        }
        
        protected void grvAirports_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvAirport.PageIndex = e.NewPageIndex;
            grvAirport.DataBind();
            BindAirportsSearch();
        }

        protected void grvAirports_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int id = int.Parse(grvAirport.DataKeys[e.NewEditIndex].Value.ToString());
            Response.Redirect("AirportManagementEdit.aspx?Id=" + id);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindAirportsSearch();
        }
        protected void grvAirports_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = int.Parse(grvAirport.DataKeys[e.RowIndex].Value.ToString());
            AirPorts air = new AirPorts(id);
            air.IsDeleted = true;
            air.Save();
            BindAirportsSearch();
        }
        protected void grvAirports_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int id = int.Parse(grvAirport.DataKeys[e.RowIndex].Value.ToString());
            AirPorts air = new AirPorts(id);
            air.IsDeleted = false;
            air.Save();
            BindAirportsSearch();
        }
    }
}